#!/usr/bin/env python
# coding=utf-8
# Created by zhezhiyong@163.com on 2017/3/7.

import os
import os.path
from os.path import getsize
import time


def convert_text(self, text_src, delimiter, output=None):
    if not output:
        output = '%s.svm' % text_src
    if isinstance(text_src, list):
        with open(output, 'w') as fw:
            for src in text_src:
                with open(src, 'r') as fr:
                    while 1:
                        line = fr.readline()
                        if not line:
                            break
                        try:
                            label, text = line.split(delimiter)
                        except ValueError:
                            continue
                        feat, label = self.to_svm(text, label)
                        fw.write('%s %s\n' % (label, ''.join(' {0}:{1}'.format(f, feat[f]) for f in sorted(feat))))


def convert_text1(self, text_src, delimiter, output=None):
    if not output:
        output = '%s.svm' % text_src
    if isinstance(text_src, str):
        with open(text_src, 'r') as f:
            text_src = [line.split(delimiter) for line in f]
    elif not isinstance(text_src, list):
        raise TypeError('text_src should be list or str')
    with open(output, 'w') as w:
        for line in text_src:
            try:
                label, text = line
            except ValueError:
                continue
            feat, label = self.to_svm(text, label)
            w.write('%s %s\n' % (label, ''.join(' {0}:{1}'.format(f, feat[f]) for f in sorted(feat))))


# 一行一行读取文件
def read_txt_line(txt_src):
    pos = 0
    with open(txt_src) as f:
        line = f.readline()
        if not line:
            return
        else:
            line = line.decode('gb2312', 'ignore').encode('utf-8')
            line = strip_str(line)
            if len(line) > 30:
                tmp2 = [theme, line]
                tmp.append(tmp2)
            return


def read_txt(txt, theme):
    tmp = []
    file_obj = open(txt)
    try:
        while 1:
            line = file_obj.readline()
            if not line:
                break
            line = line.decode('gb2312', 'ignore').encode('utf-8')
            line = strip_str(line)
            if len(line) > 30:
                tmp2 = [theme, line]
                tmp.append(tmp2)
    finally:
        file_obj.close()
    return tmp


def write_txt(text):
    file_name = 'data/test-%s.txt' % time.time()
    fr = open(file_name, 'a')
    for line in text:
        row = line[0] + ':' + line[1]
        fr.write(row)
    fr.close()


def strip_str(line):
    p1 = line.find('【')
    p2 = line.find('】')
    if p1 < 0 or p2 < 0 or p1 > p2:
        return line
    r = line[:p1] + line[p2 + 3:]
    return strip_str(r)


def dir_list(root_dir=None):
    file_list = []
    if root_dir is None:
        root_dir = u'C:\\Users\\User\\Desktop\\营销'  # 指明被遍历的文件夹
    for parent, dirnames, filenames in os.walk(root_dir):  # 三个参数：分别返回1.父目录 2.所有文件夹名字（不含路径） 3.所有文件名字
        # for dirname in dirnames:  # 输出文件夹信息
        # print "parent is:" + parent
        # print "dirname is" + dirname

        for filename in filenames:  # 输出文件信息
            # print "parent is: " + parent
            # print "filename is:" + filename
            # print "the full name of the file is:" + os.path.join(parent, filename)  # 输出文件路径信息
            file_list.append(os.path.join(parent, filename))
    return file_list


def traverse_file_to_txt():
    file_list = dir_list()
    for file in file_list:
        result = read_txt(file, '营销')
        write_txt(result)
        size = getsize(file)
        print 'There are %.3f' % (size / 1024 / 1024), 'Mbytes in txt'


if __name__ == "__main__":
    # result = read_txt(u'TrainningSet/快递/中通.txt', '快递')
    # result2 = read_txt(u'TrainningSet/快递/格格小区原内容.txt', '快递')
    # result3 = read_txt(u'TrainningSet/营销/点对点营销.txt', '营销')
    # result4 = read_txt(u'TrainningSet/验证码/验证码订单内容.txt', '验证码')
    # result5 = read_txt(u'TrainningSet/验证码/验证码订单内容2.txt', '验证码')
    # print len(result)
    # result.extend(result2)
    # print len(result)
    # result.extend(result3)
    # print len(result)
    # result.extend(result4)
    # print len(result)
    # result.extend(result5)
    # print len(result)
    # write_txt(result)
    # traverse_file_to_txt()
    pass
